package com.zzgele.qmzb.api.web;

import com.alibaba.boot.dubbo.annotation.DubboConsumer;
import com.github.pagehelper.PageInfo;
import com.zzgele.qmzb.api.core.Result;
import com.zzgele.qmzb.api.core.ResultGenerator;
import com.zzgele.qmzb.api.model.Black;
import com.zzgele.qmzb.api.model.User;
import com.zzgele.qmzb.api.service.BlackService;
import com.zzgele.qmzb.api.service.UserService;
import com.zzgele.qmzb.api.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Condition;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Created by Martin on 2017/07/13.
*/
@RestController
@RequestMapping("/black")
public class BlackController {
    private final Logger logger = LoggerFactory.getLogger(BlackController.class);
    @DubboConsumer
    private BlackService blackService;
    @DubboConsumer
    private UserService userService;

    @PostMapping("/create")
    public Result create(Long login_uid, Long black_uid) {
        if(black_uid == login_uid) {
            return ResultGenerator.genFailResult("不能把自己拉黑");
        }

        User userRefer = userService.findBy("id", black_uid);
        if (userRefer == null) {
            return ResultGenerator.genFailResult("对象不存在或已禁用");
        }

        Condition condition = new Condition(Black.class);
        condition.createCriteria().andCondition("uid=" + login_uid + " AND black_uid=" + black_uid);
        List<Black> blackList = blackService.findByCondition(condition);
        if (blackList.size() != 0) {
            return ResultGenerator.genSuccessResult("重复添加成功");
        }

        Black black = new Black();
        black.setUid(login_uid);
        black.setBlackUid(black_uid);
        blackService.save(black);

        return ResultGenerator.genSuccessResult("添加成功");
    }

    @PostMapping("/list")
    public Result list(Long login_uid, Integer page, Integer page_size) {

        PageUtil.startPage(page, page_size);
        Map<String, Object> map = new HashMap();
        map.put("uid", login_uid);
        List<HashMap> blackList = blackService.getList(map);
        HashMap blackListHashMap = PageUtil.getPageInfoHashMap(new PageInfo(blackList));

        return ResultGenerator.genSuccessResult(blackListHashMap);
    }

    @PostMapping("/clear")
    public Result clear(Long login_uid) {
        //Condition condition = new Condition(Black.class);
        //condition.createCriteria().andCondition("uid=" + userId);
        ////condition.setOrderByClause("name desc");
        //List<Black> blackList = blackService.findByCondition(condition);
        //return ResultGenerator.genSuccessResult(blackList);

        Condition condition = new Condition(Black.class);
        condition.createCriteria().andCondition("uid=" + login_uid);

        int count = blackService.deleteByCondition(condition);

        return ResultGenerator.genSuccessResult("清空成功");
    }

    @PostMapping("/delete")
    public Result delete(Long login_uid, Long black_uid) {
        Condition condition = new Condition(Black.class);
        condition.createCriteria().andCondition("uid=" + login_uid + " AND black_uid=" + black_uid);

        int count = blackService.deleteByCondition(condition);

        return ResultGenerator.genSuccessResult("删除成功");
    }

}
